package com.dormitory.management.system.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.req.BuildingPageReq;
import com.dormitory.management.system.req.BuildingSaveReq;
import com.dormitory.management.system.req.BuildingUpdateReq;

import com.dormitory.management.system.resp.BuildingResp;
import com.dormitory.management.system.service.BuildingService;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 楼栋表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "楼栋管理")
@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    @ApiOperation("添加楼栋")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody BuildingSaveReq req){
        if(!buildingService.save(req)) {
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    @ApiOperation("删除楼栋")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(
            @ApiParam(value = "楼栋id",required = true)
            @PathVariable Long id){
        if(!buildingService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("更新楼栋")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<String> update(@RequestBody BuildingUpdateReq req){
        if(!buildingService.update(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }

    @ApiOperation("楼栋分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<BuildingResp>> getPage(@RequestBody BuildingPageReq req){
        return Result.ok(buildingService.getPage(req));
    }
}
